(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.trustedSrc.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'modalService',
			'trustedSrcService' ];
	function deleteController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, modalService, trustedSrcService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.trustedSrc = {};
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.trustedSrcSites.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.trustedSrc.trustedSrcSites';
			ctrl.innerGridOptions.verifyData = 'vm.trustedSrcOld.trustedSrcSites';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			trustedSrcService.initDataOptions(ctrl);
			authService.delete(params).then(function(response) { 
				ctrl.trustedSrc = response.data; 
				ctrl.trustedSrc.trustedSrcSites = ctrl.trustedSrc.trustedSrcSites || [];
				if (ctrl.trustedSrc.trustedSrcSites!= null && ctrl.trustedSrc.trustedSrcSites.length > 0){
					angular.forEach(ctrl.trustedSrc.trustedSrcSites, function (value, key) {
						value.dataStatus = ctrl.trustedSrc.dataStatus;
					});
				}
			});
			 
		};


		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRowsSite = selectedRows;
		}
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.trustedSrc.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.trustedSrc).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};

		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.trustedSrc.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.trustedsrc.list',
					'state' : 'cda.trustedSrc.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
